package 课设;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class manager_select extends JFrame {//查看和修改

    private JTextField title;
    private JTextArea content;
    private JButton save;

    public manager_select(int blogId) throws SQLException {

        this.setSize(600,800);
        this.setTitle("编辑博客");
        this.setLocationRelativeTo(null);

        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        this.getContentPane().add(splitPane);

        db dbcon = new db();
        String sql = "select * from detailed_blog where blogId = " + blogId;
        ResultSet set = dbcon.executeQuery(sql);
        String t = "", c = "";
        if(set.next()){
            t = set.getString("article");
            c = set.getString("content");
        }


        splitPane.add(title = new JTextField(t,1));

        splitPane.setDividerLocation(50);
        splitPane.setOneTouchExpandable(true);

        BorderLayout a = new BorderLayout();
        JPanel x = new JPanel(a);
        splitPane.add(x);

        content = new JTextArea(c,40, 50);
        content.setWrapStyleWord(true);
        content.setLineWrap(true);

        JScrollPane msgScroller = new JScrollPane();
        msgScroller.setBorder(
                BorderFactory.createTitledBorder("正文"));
        msgScroller.setViewportView(content);

        x.add(msgScroller);

        save = new JButton("保存");
        x.add(save, BorderLayout.SOUTH);
        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                db dbcon = new db();
                try {
                        String sql1 = "update detailed_blog set article = ? , content = ? where blogId = " + blogId;//id,title,summary,post,userId
                        PreparedStatement prestate = null;
                        String s = title.getText();
                        prestate = dbcon.PreparedStatement(sql1);
                        prestate.setString(1,s);
                        prestate.setString(2,content.getText());
                        prestate.executeUpdate();

                        String sql2 = "update blog set title = ? , summary = ? where blogId = " + blogId;
                        PreparedStatement p = dbcon.PreparedStatement(sql2);
                        p.setString(1,s);

                        String summary = "";
                        if(content.getText().length() > 50)
                            summary = content.getText().substring(0,50) + "...";
                        else
                            summary = content.getText();
                        p.setString(2,summary);
                        p.executeUpdate();

                        dispose();
                        blog_show psel = new blog_show("");
                        psel.setVisible(true);

                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
    public static void main(String[] agrs) throws SQLException {
        bolg_select_update w = new bolg_select_update(1,1,"张三");
        w.setVisible(true);
    }
}

